#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>

using namespace std;

struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode() : val(0), left(nullptr), right(nullptr) {}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
	TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
	int findBottomLeftValue(TreeNode* root) {
		vector<vector<int>> result;
		vector<int> path;
		queue<TreeNode*> q;
		q.push(root);
		TreeNode* cur = root;
		while (!q.empty())
		{
			int len = q.size();
			for (int i = 0; i < len; i++)
			{
				path.push_back(q.front()->val);
				//printf("%d ", q.front()->val);
				q.pop();
				if (cur->left)
					q.push(cur->left);
				if (cur->right)
					q.push(cur->right);
				if (q.size())
					cur = q.front();
			}
			result.push_back(path);
			path.clear();


			//printf("\n");
		}
		return result[result.size() - 1][0];

	}
};
TreeNode* creat()
{
	TreeNode* n1 = new TreeNode(1);
	TreeNode* n2 = new TreeNode(2);
	TreeNode* n3 = new TreeNode(3);
	TreeNode* n4 = new TreeNode(4);
	TreeNode* n5 = new TreeNode(5);
	TreeNode* n6 = new TreeNode(6);
	TreeNode* n7 = new TreeNode(7);

	n1->left = n2;
	n1->right = n3;
	n2->left = n4;
	n3->left = n5;
	n3->right = n6;
	n5->left = n7;

	return n1;
}
int main()
{
	Solution a;
	TreeNode* root = creat();
	int ret = a.findBottomLeftValue(root);
	printf("%d\n", ret);
	return 0;
}